﻿using System;

using HL.Lib.MVC;
using HL.Lib.Models;

namespace HL.Lib.Controllers
{
    public class MNewsController : Controller
    {
        [HL.Core.MVC.PropertyInfo("Chuyên mục", "Type|News")]
        public int MenuID;

        [HL.Core.MVC.PropertyInfo("Cách hiển thị dạng", "Nhà hàng|1,Studio|2")]
        public int TypeShow;

        //[HL.Core.MVC.PropertyInfo("Vị trí", "ConfigKey|Mod.NewsState")]
        public int State;

        [HL.Core.MVC.PropertyInfo("Số lượng")]
        public int PageSize = 10;

        [HL.Core.MVC.PropertyInfo("Tiêu đề")]
        public string Title;

        public void ActionIndex(MNewsModel model)
        {

            if (ViewPage.CurrentPage.MenuID > 0)
                MenuID = ViewPage.CurrentPage.MenuID;

            int Current_MProduct_AdvMenuID = HL.Core.Global.Convert.ToInt(HL.Lib.Global.MyClass.GetCusTomPage("MNews.AdvMenuID", ViewPage.CurrentPage.Custom));
            ViewBag.Adv = ModAdvService.Instance.CreateQuery()
                    .Where(o => o.Activity == true && o.MenuID == Current_MProduct_AdvMenuID)
                    .OrderByAsc(o => o.Order)
                    .ToList_Cache();

            var dbQuery = ModNewsService.Instance.CreateQuery()
                            .Where(o => o.Activity == true)
                            .WhereIn(MenuID > 0, o => o.MenuID, WebMenuService.Instance.GetChildIDForWeb_Cache("News", MenuID, ViewPage.CurrentLang.ID))
                            .OrderByDesc(o => o.Order)
                            .Take(PageSize)
                            .Skip(PageSize * model.Page);

            //lọc tại tìm kiếm
            string filter = model.Filter;
            if (!string.IsNullOrEmpty(filter))
            {
                int[] arrPropertyID = HL.Core.Global.Array.ToInts(filter.Split('-'));
                string sFilter = string.Empty;
                for (int k = 0; arrPropertyID != null && k < arrPropertyID.Length; k++)
                {
                    int _property_select_id = arrPropertyID[k];
                    dbQuery.WhereIn(o => o.ID, ModNewsPropertyService.Instance.CreateQuery()
                                                   .Select(o => o.NewsID)
                                                   .Where(o => o.PropertyValueID == _property_select_id)
                                   );
                }
                ViewPage.ViewBag.Filter = model.Filter;
            }
            //

            ViewBag.Data = dbQuery.ToList();
            model.TotalRecord = dbQuery.TotalRecord;
            model.PageSize = PageSize;
            ViewBag.Model = model;
            ViewBag.Title = Title;
        }

        public void ActionDetail(string endCode)
        {
            MNewsModel model = new MNewsModel();
            TryUpdateModel(model);
            var entity = ModNewsService.Instance.CreateQuery()
                            .Where(o => o.Activity == true && o.Code == endCode)
                            //.WhereIn(MenuID > 0, o => o.MenuID, WebMenuService.Instance.GetChildIDForWeb_Cache("News", MenuID, ViewPage.CurrentLang.ID))
                            .ToSingle();
            ViewPage.ChangeTemplate(SysTemplateService.Instance.GetByID(4));
            if (entity != null)
            {
                //ViewBag.Other = ModNewsService.Instance.CreateQuery()
                //                        .Where(o => o.Activity == true && o.Order < entity.Order)
                //                        .WhereIn(MenuID > 0, o => o.MenuID, WebMenuService.Instance.GetChildIDForWeb_Cache("News", MenuID, ViewPage.CurrentLang.ID))
                //                        .OrderByDesc(o => o.Order)
                //                        .Take(PageSize)
                //                        .ToList();

                ViewBag.Data = entity;
                ViewBag.Title = Title;
                SetObject["view.Meta"] = entity;
                ViewBag.TypeShow = TypeShow;
                ViewPage.ViewBag.NewsID = entity.ID;
            }
            else
            { 
                ViewPage.Error404();
            }
        }

        public void ActionContactPOST(ModFeedbackEntity entity,MSendMailModel model)
        {
            if (entity.Title.Trim() == string.Empty)
                ViewPage.Message.ListMessage.Add("Nhập - tiêu đề");

            if (entity.Content.Trim() == string.Empty)
                ViewPage.Message.ListMessage.Add("Nhập - nội dung");

            if (entity.Name.Trim() == string.Empty)
                ViewPage.Message.ListMessage.Add("Nhập - tên");

            if (entity.Phone.Trim() == string.Empty)
                ViewPage.Message.ListMessage.Add("Nhập - số điện thoại");

            if (entity.Email.Trim() == string.Empty)
                ViewPage.Message.ListMessage.Add("Email chưa nhập");
            else if (Global.Utils.GetEmailAddress(entity.Email) == string.Empty)
                ViewPage.Message.ListMessage.Add("Định dạng Email không đúng");

            //hien thi thong bao loi
            if (ViewPage.Message.ListMessage.Count > 0)
            {
                string message = @"Các thông tin nhập còn thiếu hoặc chưa chính xác: \r\n";

                for (int i = 0; i < ViewPage.Message.ListMessage.Count; i++)
                    message += @"\r\n + " + ViewPage.Message.ListMessage[i];

                ViewPage.Alert(message);
            }
            else
            {
                entity.IP = HL.Core.Web.HttpRequest.IP;
                entity.Created = DateTime.Now;

                ModFeedbackService.Instance.Save(entity);

                //gửi mail
                string email = Global.Utils.GetEmailAddress(model.EmailSend);
                if (!string.IsNullOrEmpty(email))
                {
                    if (!string.IsNullOrEmpty(email) && Global.Utils.GetEmailAddress(email) != string.Empty)
                    {
                        string body = null;
                        body = HL.Lib.Global.WebResource.GetValue("Body_MailContact") + " Họ tên : " + entity.Name + "<br />" + "Địa chỉ : " + entity.Address + "<br />" + "Điện thoại : " + entity.Phone + "<br />" + "Nội dung : " + entity.Content;
                        HL.Lib.Global.Mail.SendMail(email, entity.Email.ToString(), entity.Name.ToString(), entity.Title.ToString(), body);
                    }
                }

                
                // xoa trang
                entity = new ModFeedbackEntity();

                ViewPage.Alert("Cảm ơn bạn đã gửi thông tin liên hệ cho chúng tôi !");
            }

            ViewBag.Contact = entity;
        }
    }

    public class MNewsModel
    {
        private int _Page = 0;
        public int Page
        {
            get { return _Page; }
            set { _Page = value - 1; }
        }

        public int TotalRecord { get; set; }
        public int PageSize { get; set; }

        public string Show { get; set; }
        public string Filter { get; set; }
    }
    public class MSendMailModel
    {
        public string EmailSend { get; set; }
    }
}
